package com.yyfax.common.recyclerview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yyfax.common.R;

public class HeaderRefreshView_Backup extends LinearLayout implements RefreshTrigger {
	private TextView mTextView;
	private ImageView mArrowImageView;
	private ProgressBar mProgressBar;
	private Animation mRotateUpAnim;
	private Animation mRotateDownAnim;
	private static final int ROTATE_ANIM_DURATION = 180;
	public int mMeasuredHeight;
	private boolean mFinish = false;

	public HeaderRefreshView_Backup(Context context) {
		this(context, null);
	}

	public HeaderRefreshView_Backup(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public HeaderRefreshView_Backup(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		setGravity(Gravity.CENTER);
		View view = inflate(context, R.layout.recycler_head_refresh_view, this);
		mTextView = (TextView) view.findViewById(R.id.text_view);
		mArrowImageView = (ImageView) view.findViewById(R.id.imageview);
		mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar);
		mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
		mRotateUpAnim.setFillAfter(true);
		mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
		mRotateDownAnim.setFillAfter(true);
		mMeasuredHeight = getMeasuredHeight();
	}

	@Override
	public void onStart(boolean automatic, int headerHeight, int finalHeight) {
		mFinish = true;
	}

	@Override
	public void onMove(boolean finished, boolean automatic, int moved) {
		if (finished) {
			mTextView.setText("释放立即刷新");
			if (!mFinish) {
				mProgressBar.setVisibility(INVISIBLE);
				mArrowImageView.clearAnimation();
				mArrowImageView.startAnimation(mRotateUpAnim);
				mFinish = true;
			}
		} else {
			mTextView.setText("下拉刷新");
			if (mFinish) {
				mProgressBar.setVisibility(INVISIBLE);
				mArrowImageView.clearAnimation();
				mArrowImageView.startAnimation(mRotateDownAnim);
				mFinish = false;
			}
		}
	}

	@Override
	public void onRefresh() {
		if (mFinish){
		mTextView.setText("正在刷新...");
		mArrowImageView.clearAnimation();
		mArrowImageView.setVisibility(INVISIBLE);
		mProgressBar.setVisibility(VISIBLE);
		mFinish = false;
		}
	}

	@Override
	public void onRelease() {
		if (mFinish){
			mTextView.setText("正在刷新...");
			mArrowImageView.clearAnimation();
			mArrowImageView.setVisibility(INVISIBLE);
			mProgressBar.setVisibility(VISIBLE);
			mFinish = false;
		}
	}

	@Override
	public void onComplete() {

	}

	@Override
	public void onReset() {
		mArrowImageView.clearAnimation();
		mArrowImageView.setVisibility(VISIBLE);
		mProgressBar.setVisibility(INVISIBLE);
		mArrowImageView.startAnimation(mRotateDownAnim);
		mTextView.setText("下拉刷新");

	}
}